import { get, post, put, del } from '@/utils/request'

// 获取服务列表
export function getServicesList(params = {}) {
  return get('/services', params)
}

// 获取服务详情
export function getServiceDetail(id) {
  return get(`/services/${id}`)
}

// 创建服务
export function createService(data) {
  return post('/services', data)
}

// 更新服务
export function updateService(id, data) {
  return put(`/services/${id}`, data)
}

// 删除服务
export function deleteService(id) {
  return del(`/services/${id}`)
}

// 上架/下架服务
export function toggleServiceStatus(id, status) {
  return put(`/services/${id}/status`, { status })
}

// 获取服务统计
export function getServiceStats() {
  return get('/services/stats')
}

// 保存草稿
export function saveDraft(data) {
  return post('/services/draft', data)
}

// 获取草稿列表
export function getDraftList() {
  return get('/services/drafts')
}

// 提交审核
export function submitForReview(id) {
  return put(`/services/${id}/submit`)
}

// 上传服务图片
export function uploadServiceImage(file) {
  const formData = new FormData()
  formData.append('file', file)
  return post('/services/upload', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 获取服务类型配置
export function getServiceTypes() {
  return get('/services/types')
}

// 检查押金状态
export function checkDepositStatus(serviceType) {
  return get(`/services/deposit/check`, { serviceType })
}

// 支付押金
export function payDeposit(serviceType, amount) {
  return post('/services/deposit/pay', { serviceType, amount })
}

// 获取推广数据
export function getPromotionData(serviceId) {
  return get(`/services/${serviceId}/promotion`)
}

// 开启/关闭推广
export function togglePromotion(serviceId, enabled) {
  return put(`/services/${serviceId}/promotion`, { enabled })
}

// 获取服务分析数据
export function getServiceAnalysis(type, timeRange, customRange = null) {
  const params = { type, timeRange }
  if (customRange) {
    params.startDate = customRange[0]
    params.endDate = customRange[1]
  }
  return get('/services/analysis', params)
}

// 获取服务排行榜
export function getServiceRanking(params = {}) {
  return get('/services/ranking', params)
}

// 获取服务订单列表
export function getServiceOrders(serviceId, params = {}) {
  return get(`/services/${serviceId}/orders`, params)
}

// 批量操作服务
export function batchUpdateServices(serviceIds, action, data = {}) {
  return put('/services/batch', {
    serviceIds,
    action, // 'activate', 'deactivate', 'delete', 'update'
    data
  })
}

// 导出服务数据
export function exportServicesData(params = {}) {
  return get('/services/export', params, {
    responseType: 'blob'
  })
}

// 获取服务模板
export function getServiceTemplates() {
  return get('/services/templates')
}

// 复制服务
export function copyService(serviceId, data = {}) {
  return post(`/services/${serviceId}/copy`, data)
}

// 获取服务推广统计
export function getPromotionStats(serviceId, timeRange = '30d') {
  return get(`/services/${serviceId}/promotion/stats`, { timeRange })
}

// 获取服务详细分析
export function getServiceDetailAnalysis(serviceId, params = {}) {
  return get(`/services/${serviceId}/analysis`, params)
}

// 搜索服务
export function searchServices(keyword, filters = {}) {
  return get('/services/search', {
    keyword,
    ...filters
  })
}

// 获取服务配置
export function getServiceConfig() {
  return get('/services/config')
}

// 更新服务配置
export function updateServiceConfig(config) {
  return put('/services/config', config)
}

// 获取押金状态列表
export function getDepositStatusList() {
  return get('/services/deposit/status')
}

// 获取服务审核历史
export function getServiceAuditHistory(serviceId) {
  return get(`/services/${serviceId}/audit-history`)
}

// 申请服务推广
export function applyPromotion(serviceId, promotionData) {
  return post(`/services/${serviceId}/promotion/apply`, promotionData)
}

// 获取推广申请状态
export function getPromotionApplicationStatus(serviceId) {
  return get(`/services/${serviceId}/promotion/application`)
}

// 获取服务洞察数据
export function getServiceInsights(params = {}) {
  return get('/services/insights', params)
}

// 获取服务性能报告
export function getServicePerformanceReport(serviceId, timeRange = '30d') {
  return get(`/services/${serviceId}/performance`, { timeRange })
}

// 获取竞争对手分析
export function getCompetitorAnalysis(serviceId) {
  return get(`/services/${serviceId}/competitor-analysis`)
}

// 获取服务优化建议
export function getServiceOptimizationSuggestions(serviceId) {
  return get(`/services/${serviceId}/optimization-suggestions`)
}

// 获取服务图片上传token
export function getUploadToken() {
  return get('/services/upload/token')
}

// 上传服务图片
export function uploadServiceImages(files, serviceId) {
  const formData = new FormData()
  files.forEach((file, index) => {
    formData.append(`images[${index}]`, file)
  })
  formData.append('serviceId', serviceId)

  return post('/services/upload/images', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    showLoading: true,
    loadingText: '上传图片中...'
  })
}

// 删除服务图片
export function deleteServiceImage(serviceId, imageId) {
  return del(`/services/${serviceId}/images/${imageId}`)
}

// 获取服务评价列表
export function getServiceReviews(serviceId, params = {}) {
  return get(`/services/${serviceId}/reviews`, params)
}

// 回复服务评价
export function replyServiceReview(serviceId, reviewId, reply) {
  return post(`/services/${serviceId}/reviews/${reviewId}/reply`, { reply })
}

// 获取服务FAQ
export function getServiceFAQ(serviceId) {
  return get(`/services/${serviceId}/faq`)
}

// 更新服务FAQ
export function updateServiceFAQ(serviceId, faqData) {
  return put(`/services/${serviceId}/faq`, faqData)
}

// 获取服务标签建议
export function getServiceTagSuggestions(serviceType) {
  return get('/services/tag-suggestions', { serviceType })
}

// 获取价格建议
export function getPriceSuggestions(serviceType, area) {
  return get('/services/price-suggestions', { serviceType, area })
}

// 检查服务名称可用性
export function checkServiceNameAvailability(name, excludeId = null) {
  return get('/services/check-name', { name, excludeId })
}

// 获取服务质量评分
export function getServiceQualityScore(serviceId) {
  return get(`/services/${serviceId}/quality-score`)
}

// 申请服务认证
export function applyServiceCertification(serviceId, certificationData) {
  return post(`/services/${serviceId}/certification`, certificationData)
}

// 获取服务认证状态
export function getServiceCertificationStatus(serviceId) {
  return get(`/services/${serviceId}/certification/status`)
}

// 获取服务推广效果分析
export function getPromotionEffectAnalysis(serviceId, timeRange = '30d') {
  return get(`/services/${serviceId}/promotion/effect-analysis`, { timeRange })
}

// 预测服务需求
export function predictServiceDemand(serviceId, timeRange = '7d') {
  return get(`/services/${serviceId}/demand-prediction`, { timeRange })
}

// 获取服务热力图数据
export function getServiceHeatmapData(serviceId, timeRange = '30d') {
  return get(`/services/${serviceId}/heatmap`, { timeRange })
}

// 获取服务客户画像
export function getServiceCustomerProfile(serviceId) {
  return get(`/services/${serviceId}/customer-profile`)
}

// 获取服务转化漏斗
export function getServiceConversionFunnel(serviceId, timeRange = '30d') {
  return get(`/services/${serviceId}/conversion-funnel`, { timeRange })
}

// 服务状态枚举
export const SERVICE_STATUS = {
  DRAFT: 'DRAFT',           // 草稿
  PENDING: 'PENDING',       // 待审核
  APPROVED: 'APPROVED',     // 已通过
  REJECTED: 'REJECTED',     // 已驳回
  ACTIVE: 'ACTIVE',         // 已上架
  INACTIVE: 'INACTIVE',     // 已下架
  BANNED: 'BANNED'          // 已禁用
}

// 服务状态文本映射
export const SERVICE_STATUS_TEXT = {
  [SERVICE_STATUS.DRAFT]: '草稿',
  [SERVICE_STATUS.PENDING]: '待审核',
  [SERVICE_STATUS.APPROVED]: '已通过',
  [SERVICE_STATUS.REJECTED]: '已驳回',
  [SERVICE_STATUS.ACTIVE]: '已上架',
  [SERVICE_STATUS.INACTIVE]: '已下架',
  [SERVICE_STATUS.BANNED]: '已禁用'
}

// 服务状态颜色映射
export const SERVICE_STATUS_COLOR = {
  [SERVICE_STATUS.DRAFT]: 'info',
  [SERVICE_STATUS.PENDING]: 'warning',
  [SERVICE_STATUS.APPROVED]: 'success',
  [SERVICE_STATUS.REJECTED]: 'danger',
  [SERVICE_STATUS.ACTIVE]: 'success',
  [SERVICE_STATUS.INACTIVE]: 'info',
  [SERVICE_STATUS.BANNED]: 'danger'
}

// 服务类型配置
export const SERVICE_TYPES = {
  LAUNDRY: 'LAUNDRY',           // 洗衣服
  SHOES: 'SHOES',               // 洗鞋
  PET: 'PET',                   // 洗萌宠
  BAG: 'BAG',                   // 洗包包
  BEDDING: 'BEDDING',           // 洗被子
  CAR: 'CAR',                   // 洗车
  HOME_CLEANING: 'HOME_CLEANING', // 上门清洗
  PLUMBING: 'PLUMBING'          // 疏通厕所
}

// 服务类型文本映射
export const SERVICE_TYPE_TEXT = {
  [SERVICE_TYPES.LAUNDRY]: '洗衣服',
  [SERVICE_TYPES.SHOES]: '洗鞋',
  [SERVICE_TYPES.PET]: '洗萌宠',
  [SERVICE_TYPES.BAG]: '洗包包',
  [SERVICE_TYPES.BEDDING]: '洗被子',
  [SERVICE_TYPES.CAR]: '洗车',
  [SERVICE_TYPES.HOME_CLEANING]: '上门清洗',
  [SERVICE_TYPES.PLUMBING]: '疏通厕所'
}

// 押金配置
export const DEPOSIT_CONFIG = {
  [SERVICE_TYPES.LAUNDRY]: 500,      // 洗衣服押金500元
  [SERVICE_TYPES.SHOES]: 300,        // 洗鞋押金300元
  [SERVICE_TYPES.PET]: 800,          // 洗萌宠押金800元
  [SERVICE_TYPES.BAG]: 400,          // 洗包包押金400元
  [SERVICE_TYPES.BEDDING]: 600,      // 洗被子押金600元
  [SERVICE_TYPES.CAR]: 1000,         // 洗车押金1000元
  [SERVICE_TYPES.HOME_CLEANING]: 1200, // 上门清洗押金1200元
  [SERVICE_TYPES.PLUMBING]: 800      // 疏通厕所押金800元
}

// 计价单位选项
export const PRICING_UNITS = [
  { label: '件', value: '件' },
  { label: '双', value: '双' },
  { label: '只', value: '只' },
  { label: '套', value: '套' },
  { label: '次', value: '次' },
  { label: '小时', value: '小时' },
  { label: '平方米', value: '平方米' },
  { label: '公斤', value: '公斤' }
]
